package bb.lanxing.view;

import java.lang.reflect.Field;

import android.content.Context;
import android.content.DialogInterface;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import bb.lanxing.R;
import bb.lanxing.util.DensityUtil;
import bb.lanxing.util.Log;

public class BiciAlertDialogBuilder extends AlertDialog.Builder {
    private int height;
    private int width;

    private Context mContext = null;

    public BiciAlertDialogBuilder(Context context) {
        super(context);
        mContext = context;
        this.width = -1;
        this.height = -1;
    }

    public BiciAlertDialogBuilder(Context context, int i) {
        super(context, i);
        mContext = context;
        this.width = -1;
        this.height = -1;
    }

    @Override
    public AlertDialog show() {
        AlertDialog show = super.show();
        setTitleTextColor(show);
        setButtonColor(show);
        setSize(show);
        return show;
    }

    public BiciAlertDialogBuilder size(int i, int i2) {
        this.width = i;
        this.height = i2;
        return this;
    }

    private void setTitleTextColor(AlertDialog alertDialog) {
        Log.i("setTitleTextColor name " + AlertDialog.class.getName());
        try {
            Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
            mAlert.setAccessible(true);
            Object mAlertController = mAlert.get(alertDialog);

            if (mAlertController != null) {
                Field mTitle = mAlertController.getClass().getDeclaredField("mTitleView");
                mTitle.setAccessible(true);
                TextView mTitleView = (TextView) mTitle.get(mAlertController);
                if (mTitleView != null) {
                    mTitleView.setTextColor(mContext.getColor(R.color.colorPrimary));
                }
            }
        } catch (IllegalAccessException | NoSuchFieldException e) {
            Log.i("setTitleTextColor error" + e.getMessage());
        }
    }

    public void setButtonColor(AlertDialog alertDialog) {
        Button button = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
        if (button != null) {
            ((LinearLayout.LayoutParams) button.getLayoutParams()).setMargins(0, 0, DensityUtil.dp2px(16.0f), 0);
            button.setTextColor(mContext.getColor(R.color.colorPrimary));
        }
        Button button2 = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
        if (button2 != null) {
            button2.setTextColor(mContext.getColor(R.color.colorPrimary));
        }
    }

    private void setSize(AlertDialog alertDialog) {
        Window window;
        if ((this.width > 0 || this.height > 0) && (window = alertDialog.getWindow()) != null) {
            WindowManager.LayoutParams attributes = window.getAttributes();
            int i = this.height;
            if (i > 0) {
                attributes.height = i;
            }
            int i2 = this.width;
            if (i2 > 0) {
                attributes.width = i2;
            }
            window.setAttributes(attributes);
        }
    }
}
